package db_main

import (
	"fmt"
	"gorm.io/gorm"
	"longmen/server/config/global"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/db/mysql"
	"longmen/server/pkg/db/mysql/models/models_main"
)

func InsertNews(news *models_main.News) error {
	fName := "InsertNews"
	table := mysql.GetRTable(&models_main.News{})
	err := table.Create(news).Error
	if err != nil {
		return fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return nil
}
func GetNewsList(param map[string]interface{}, page int, size int, source uint32) ([]*models_main.News, error) {
	fName := "GetNewsList"
	o := make([]*models_main.News, 0)
	table := mysql.GetRTable(&models_main.News{})
	if source == 1 {
		if param != nil {
			param["own_creation"] = 1
		}
	}
	mysql.Condition(table, param, "", "publish_time DESC", page, size)
	err := table.Find(&o).Error
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}
func GetNewsByMd5hash(md5hash string) (*models_main.News, error) {
	table := mysql.GetRTable(&models_main.News{})
	news := &models_main.News{}
	err := table.Where("md5hash = ?", md5hash).Take(news).Error
	return news, err
}
func GetNewsCategoryList() ([]*models_main.NewsCategory, error) {
	fName := "GetNewsCategoryList"
	o := make([]*models_main.NewsCategory, 0)
	table := mysql.GetRTable(&models_main.NewsCategory{}).Where("disabled=?", 0)
	err := table.Find(&o).Error
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}
func GetNewsById(id int) (*models_main.News, error) {
	fName := "GetNewsById"
	table := mysql.GetRTable(&models_main.News{})
	news := &models_main.News{}

	err := table.Where("id = ?", id).Find(news).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return news, err
}
func GetBannerList() ([]*models_main.News, error) {
	fName := "GetBannerList"
	table := mysql.GetRTable(&models_main.News{})
	o := make([]*models_main.News, 0)
	mysql.Condition(table, map[string]interface{}{"is_banner": 1, "status": 1}, "", "publish_time DESC", 1, 8)
	err := table.Find(&o).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}
func UpdateBannerPublishTime(publishTime int, news *models_main.News) error {
	fName := "UpdateBannerPublishTime"
	table := mysql.GetRTable(&models_main.News{})
	err := table.Model(news).Update("publish_time", publishTime).Error

	if err != nil {
		return fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return nil
}
func UpdateNewsCategory(category int, news *models_main.News) error {
	fName := "UpdateNewsCategory"
	table := mysql.GetRTable(&models_main.News{})
	err := table.Model(news).Update("category_id", category).Error
	if err != nil {
		return fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return nil
}
func GetNextNews(publishTime int64, categoryId int) (*models_main.News, error) {
	fName := "GetNextNews"
	table := mysql.GetRTable(&models_main.News{})
	Nextnews := &models_main.News{}
	err := table.Where("category_Id = ? ", categoryId).Order("publish_time ASC").
		Where("publish_time > ?", publishTime).Order("id asc").
		Limit(1).
		Find(Nextnews).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	if err != nil {
		return Nextnews, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return Nextnews, nil
}
func GetLastNews(publishTime int64, categoryId int) (*models_main.News, error) {
	fName := "GetLastNews"
	table := mysql.GetRTable(&models_main.News{})
	lastnews := &models_main.News{}
	err := table.Where("category_Id = ? ", categoryId).Order("publish_time DESC").Where("publish_time < ?", publishTime).
		Order("id asc").
		Limit(1).
		Find(lastnews).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	if err != nil {
		return lastnews, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return lastnews, nil
}
func GetNextNewsByIsHot(publishTime int64, isHot int) (*models_main.News, error) {
	fName := "GetNextNewsByIsHot"
	table := mysql.GetRTable(&models_main.News{})
	Nextnews := &models_main.News{}
	err := table.Where("is_hot = ? ", isHot).Order("publish_time ASC").Where("publish_time > ?", publishTime).Order("id asc").
		Limit(1).
		Find(Nextnews).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	if err != nil {
		return Nextnews, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return Nextnews, nil
}
func GetLastNewsByIsHot(publishTime int64, isHot int) (*models_main.News, error) {
	fName := "GetLastNewsByIsHot"
	table := mysql.GetRTable(&models_main.News{})
	lastnews := &models_main.News{}
	err := table.Where("is_hot = ? ", isHot).Order("publish_time DESC").
		Where("publish_time < ?", publishTime).Order("id asc").
		Limit(1).
		Find(lastnews).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	if err != nil {
		return lastnews, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return lastnews, nil
}

func IncrNewHits(news *models_main.News) {
	err := mysql.GetRTable(&models_main.News{}).Where("id = ?", news.ID).Updates(models_main.News{Hits: news.Hits + 1}).Error
	if err != nil {
		global.Log.Error("IncrNewHits err:%v", err)
	}
}
